Git and GitHub Tutorial
Overview of Git and GitHub
At a high-level, what are git and GitHub?
- git: a version control system that allows you to track changes in your code
- GitHub: a platform that allows you to host your git repositories online/remotely
There many possible starting points for creating/initializing a GitHub repository:
- Start with an existing remote repository from GitHub;
- Create a new remote repository on GitHub; or
- Start with an existing local repository on your computer.
In this walkthrough, we will be setting up two GitHub repositories:
dsip-s26: repository with course materials (lectures, code, etc.)- To set up this
dsip-s26repository, we will use option (A) above. - You won’t be interacting with this repository much besides pulling to receive course materials.
- To set up this
dsip: your repository for your own work (e.g., labs, final project)- To set up this
dsiprepository, we will use option (B) above. - This is the repository that you will be interacting with the most.
- To set up this
Instructions to set up the dsip-s26 repository
In your terminal:
- Navigate to the directory where you want to store the course materials, e.g.,
cd path/to/directory- Clone the
dsip-s26repository by running the following command:
git clone https://github.com/tiffanymtang/dsip-s26.gitNote: This will create a new directory called
dsip-s26in your current working directory. To see this, you can runls
- To update the course materials at any point during the semester, you should navigate into the
dsip-s26directory, e.g.,
cd dsip-s26and run
git pullOpen GitKraken and click on the “Clone a repo” button.
In the URL field, enter the following URL: https://github.com/tiffanymtang/dsip-s26. You can select where you want to store this repository on your computer by clicking on the “Browse” button next to “Where to clone to”. Once you are satisfied with the location, click on the “Clone the repo!” button.
If a pop-up appears asking you whether to open the
dsip-s26repository, go ahead and click on the “Open Now” button.To update the course materials at any point during the semester, click on the “Pull” button at the top of the application.
Instructions to set up your dsip repository
Next, we will create your personal dsip repository that you will be using to work on your labs. Unlike the dsip-s26 repository which was already an existing GitHub repository (and thus you only had to clone it locally), you will be creating your dsip repository from scratch on GitHub.
Go to: https://github.com/ and log in.
Click on the green “New” button (on the left) to create a new repository.
Fill in the following information:
- Owner: your GitHub username
- Repository name:
dsip - Public or Private: Please choose “Private” so that only you (and your added collaborators) can see your repository.
- Initialize this repository with: I would recommend checking the box for “Add a README file” so that you can easily clone the repository to your computer.
- Add .gitignore: For now, you can leave this as “None”.
- Add a license: I would recommend selecting “MIT License” from the dropdown menu, but this is optional.
Click on the green “Create repository” button.
Once you have created the repository, you will be taken to the repository’s main page. We next need to “clone” the (remote) repository to our local computers like we did with the
dsip-s26repository. So following the same steps from before:
In your terminal:
- Navigate to the directory where you want to store your
dsiprepository, e.g.,
cd path/to/directory- Clone the
dsiprepository by running the following command:
git clone https://github.com/{your_github_username}/dsip.gitNote: This will create a new directory called
dsipin your current working directory. To see this, you can runls
Open GitKraken and click on the “Clone a repo” button.
In the URL field, enter the following URL: https://github.com/{your_github_username}/dsip. You can select where you want to store this repository on your computer by clicking on the “Browse” button next to “Where to clone to”. Once you are satisfied with the location, click on the “Clone the repo!” button.
If a pop-up appears asking you whether to open the
dsiprepository, go ahead and click on the “Open Now” button.
So far, we’ve set up two different GitHub repositories. Next, using your dsip repository, we will go over how to interact/make changes to these repositories and to push these changes to GitHub.
A typical GitHub workflow
A typical GitHub workflow involves the following four commands:
- First,
git pullto download changes from the remote GitHub repository to your local computer - After making changes to your local repository,
git addfiles that you’d like to stage for your next commit - Next,
git committo store a “snapshot” of these added changes in your git version history - Finally,
git pushto upload these local changes to the remote GitHub repository
To see this workflow in action, let’s make a minor change to our dsip repository. In particular, let’s create a new text file called info.txt that contains the following two lines:
name = "Your Name"
github_name = "Your GitHub Username"
Please place this info.txt file in your dsip folder (i.e., the file path should be dsip/info.txt).
Let’s now go through the four steps of the GitHub workflow. We will look at the equivalent commands using terminal, GitHub Desktop, and GitKraken side-by-side.
Terminal
- Navigate to the desired repository (i.e., your
dsiprepository):
cd path/to/dsipGitKraken
Navigate to the desired repository (i.e., your
dsiprepository):Open your
dsiprepository in GitKraken (e.g., using the “Browse for a repo” button).
- To pull:
git pullRecall: “pulling” is the process of downloading changes from the remote GitHub repository to your local computer.
- To add modified/new files to staging area:
git add info.txtYou may want to check the status of your git repository using
git statusto see which files have been modified and/or added to the staging area. It is common to rungit statusbefore and/or after each step of this workflow when first learning git.
- To commit staged files (with message/description):
git commit -m "add info.txt"To commit staged files (with message/description):
Add a commit message to the “Commit summary” field. Once you are satisfied with the message, click on the “Commit changes” button.
Tip: It is good practice to keep your commits modular and focused (e.g., they should address one bug or add one feature to your code). This will make it easier to track version changes and to revert back to previous versions if needed. To help facilitate this, you should also try to write informative commit messages that describe the changes you made in the commit.
- To push:
git pushTo push:
Click on the “Push” button at the top of the application. After you click on “Push”, the head of the local repository (computer icon) and the head of the remote repository (your GitHub icon) should be aligned at the same commit.
Recall: “pushing” is the process of uploading changes from your local computer to the remote GitHub repository. If you do not push your changes, they will not be reflected on GitHub and not accessible to collaborators.
Lastly, please add tiffanymtang as a collaborator in your dsip repository (so that I can view your lab submissions). To do this, please:
- Go to your
dsiprepository on GitHub: https://github.com/{your_github_username}/dsip - Go to Settings (on the top) > Collaborators (on the left) > Add people (the green button) > Enter
tiffanymtang> Click on “Add tiffanymtang to this repository”.
Git Cheat Sheet
For a quick reference guide to common git/GitHub commands, please refer to this GitHub Cheat Sheet.